package me.exchange.com.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.exchange.com.aop.log.Log;
import me.exchange.com.config.DataScope;
import me.exchange.com.exception.BadRequestException;
import me.exchange.com.modules.system.domain.Dept;
import me.exchange.com.modules.system.dto.DeptDto;
import me.exchange.com.modules.system.dto.DeptQueryCriteria;
import me.exchange.com.modules.system.service.DeptService;
import me.exchange.com.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Zheng Jie
* @date 2019-03-25
*/
@RestController
@Api(tags = "系统：部门管理")
@RequestMapping("/api/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    private static final String ENTITY_NAME = "sys_dept";


    @Log("导出部门数据")
    @ApiOperation("导出部门数据")
    @PostMapping(value = "/list/download")
    @PreAuthorize("@el.check('dept:list')")
    public void download(HttpServletResponse response, DeptQueryCriteria criteria) throws IOException {
        deptService.download(deptService.queryAll(criteria), response);
    }

    @Log("查询部门")
    @ApiOperation("查询部门")
    @PostMapping("/list")
    @PreAuthorize("@el.check('user:list','dept:list')")
    public ResponseEntity<Object> getDepts( DeptQueryCriteria criteria,Pageable pageable){
        // 数据权限
        //criteria.setId(dataScope.getDeptIds());
        List<DeptDto> deptDtos = deptService.queryAll(criteria);
        return new ResponseEntity<>(Result.ok("查询成功").put("deptList",deptDtos),HttpStatus.OK);
    }

    @Log("查询部门下拉列表")
    @ApiOperation("查询部门下拉列表")
    @PostMapping("/deptSelect")
    @PreAuthorize("@el.check('user:list','dept:list')")
    public ResponseEntity<Object> getAllDepts(DeptQueryCriteria criteria){
        if(criteria.getCAdminTime() != null && criteria.getCAdminTime().size()>0){
            if(criteria.getCAdminTime().get(1).before(criteria.getCAdminTime().get(0))){
                return new ResponseEntity<>(Result.error("操作失败，查询时间段设置不正确"),HttpStatus.OK);
            }
        }
        List<DeptDto> deptDtos = deptService.queryAll(criteria);
        return new ResponseEntity<>(Result.ok("部门下拉查询成功").put("deptList",deptDtos),HttpStatus.OK);
    }

    @PostMapping("/queryInfo")
    @Log("查询部门详情")
    @ApiOperation("查询部门详情")
    @PreAuthorize("@el.check('dept:list')")
    public ResponseEntity<Object> queryInfo(Long id){
        DeptDto deptDto =  deptService.findById(id);
        return new ResponseEntity<>(Result.ok("部门详情查询成功").put("dept",deptDto),HttpStatus.OK);
    }
    @Log(value="新增部门",code="system_dept")
    @ApiOperation("新增部门")
    @PostMapping("/add")
    @PreAuthorize("@el.check('dept:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Dept resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        DeptDto dto=deptService.create(resources);
        return new ResponseEntity<>(Result.ok("部门添加成功").put("dept",dto),HttpStatus.OK);
    }

    @Log(value="修改部门",code="system_dept")
    @ApiOperation("修改部门")
    @PostMapping("/update")
    @PreAuthorize("@el.check('dept:edit')")
    public ResponseEntity<Object> update(@Validated(Dept.Update.class) @RequestBody Dept resources){
        deptService.update(resources);
        return new ResponseEntity<>(Result.ok("部门修改成功"),HttpStatus.OK);
    }

    @Log(value="部门状态修改",code="system_dept")
    @ApiOperation("部门状态修改")
    @PostMapping("/updateStatus")
    @PreAuthorize("@el.check('dept:edit')")
    public ResponseEntity<Object> updateStatus(@RequestBody DeptQueryCriteria criteria){

        deptService.updateStatus(criteria);
        return new ResponseEntity<>(Result.ok("状态修改成功"),HttpStatus.OK);
    }

    @Log(value="删除部门",code="system_dept")
    @ApiOperation("删除部门")
    @PostMapping("/del")
    @PreAuthorize("@el.check('dept:del')")
    public ResponseEntity<Object> delete(@RequestBody DeptQueryCriteria criteria){
        Set<DeptDto> deptDtos = new HashSet<>();
        for (Long id : criteria.getIds()) {
            deptDtos.add(deptService.findById(id));
        }
        deptService.delete(deptDtos);
        return new ResponseEntity<>(Result.ok("删除成功"),HttpStatus.OK);
    }
}